package top.kaoshanji.q1ch07a;

import top.kaoshanji.y1constant.FileConstant;

import java.io.*;

/**
 * 代码清单-q1ch07a-332页a：存储和恢复数据
 * 十六进制的编码
 * author kaoshanji
 * ^_^
 * create time 2025/3/12 16:10
 */
public class StoringAndRecoveringData {

    static String pathStr = FileConstant.LOCAL_ROOT_PATH.concat("q1ch07a332页a\\Data.txt");

    public static void main(String[] args) {

        // 向文件里写入数据
        try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(pathStr)))){

            out.writeDouble(3.14159);
            out.writeUTF("我是靓仔");
            out.writeDouble(1.41413);
            out.writeUTF("第二行数据");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 从文件中读取数据
        try (DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(pathStr)))){

            System.out.println(in.readDouble());

            // 只有 readUTF() 可以正确地恢复 Java 的 UTF 字符串
            System.out.println(in.readUTF());
            System.out.println(in.readDouble());
            System.out.println(in.readUTF());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }


}
